<?php
# 文件名称:statistics.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class statisticsModel extends C_model {
	var $_lists   = array();
	
	var $_where  = '';
	
	var $_fdparameter = null;
	
	
    function statisticsModel()
    {
        parent::C_model();
    }
	function getExports()
	{
	}
	//反馈记录集
	function getItems()
	{
		if (!empty( $this->_data ))
		{
			return $this->_data;
		}
		$SYS =&get_instance();
		// 获取请求的数据
		$filter_form		= $SYS->request->getVar( "filter_form",		'-1',		'post',	'int' );
		$filter_lists	= $SYS->request->getVar( 'filter_lists',	0,	'post',	'int' );
		$filter_list	= $SYS->request->getVar( 'filter_list',	0,	'post',	'int' );
		
		if($filter_form == -1) $filter_form = $SYS->request->getVar( "fdparameter_form",	'0', 'COOKIE',	'int' );
		else
		{		
			if($filter_form != $SYS->request->getVar( "fdparameter_form",	'0', 'COOKIE',	'int' ))
			{
				$limitstart = 0;
			}
		}
		setcookie('fdparameter_form', $filter_form, time()+$SYS->config->item('lifetime'), '/');
		
		//过滤查询
		$where = array();
		//权限
		$currentUser = & $SYS->session->get('admin_name');
		$defaultlang=$SYS->lang->getDefault(1,'backend');
		if(!in_array("1",explode(",",$currentUser->get('gid'))))
		{
			$condition = 'a.created_by='.$currentUser->get('id');
			$query = 'SELECT b.fieldvalue name'.
					' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
					' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
					' WHERE '.$condition.
					' AND a.id=b.recordid'.
					' AND b.module=1 AND b.fieldname="name"'.
					' AND b.lang='.$defaultlang['id']
					;
			$tmpformtitle = $this->_getArrayList($query);
			$tmptitle=array();
			for($i=0;$i<count($tmpformtitle);$i++)
			{
				$tmptitle[]= '"'.$tmpformtitle[$i]['name'].'"';				
			}
			$where[] = 'a.fdtitle IN ('.implode(",", $tmptitle).')';
		}else $condition = '1=1';
		//筛选下拉菜单
		
		//各类表单
		if($filter_form != 0)
		{
			$query = 'SELECT b.fieldvalue'.
					' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
					' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
					' WHERE a.id='.$filter_form.
					' AND '.$condition.
					' AND a.id=b.recordid'.
					' AND b.module=1 AND b.fieldname="name"'.
					' AND b.lang='.$defaultlang['id']
					;
			$this->_db->setQuery($query);
			$form = $this->_db->loadResult();
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $form, true ).'%', false );
			$where[] = 'a.fdtitle LIKE '.$searchEscaped;
			$limitstart = 0;
		}
		//各个字段参数
		$query = 'SELECT b.fieldvalue `key`'.
					' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
					' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].')'.
					' WHERE a.id='.$filter_lists.
					' AND form='.$filter_form
					;
		$this->_db->setQuery($query);
		$parameter = $this->_db->loadObject();
		if($parameter != null)
		{
			$this->_fdparameter = $parameter;
			$parameter = serialize($parameter->key);
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $parameter, true ).'%', false );
			$where[] = 'a.data like '.$searchEscaped;
			$limitstart = 0;
		}
		
		
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		$this->_where = $where;
		// 下拉菜单
		$query = 'SELECT a.id,b.fieldvalue `name`' .
				' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
				' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].')'.
				' WHERE a.use_ok=1 AND (a.type=2 OR a.type=4 OR a.type=6)'.
				' AND a.form='.$filter_form
				;
		$types[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('Item2').' -', 'id', 'name');
		$this->_db->setQuery($query);
		$types = array_merge($types, $this->_db->loadObjectList());
		$this->_lists['types'] = C_html::_('select.genericlist',  $types, 'filter_lists', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'id', 'name', $filter_lists);
		
		
		$forms[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('Item1').' -', 'id', 'name');
		$query = 'SELECT a.id,b.fieldvalue name'.
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' WHERE b.module=1 AND b.fieldname="name" AND b.lang='.$defaultlang['id'].
				' AND a.id=b.recordid '.
				' AND '.$condition;
		$forms = array_merge($forms, $this->_getObjectList( $query ));
		$this->_lists['form'] = C_html::_('select.genericlist',  $forms, "filter_form", 'class="inputbox" onchange="document.adminForm.submit( );"', 'id', 'name', $filter_form);
		$this->_form = $filter_form;
		//数据集
		if(intval($filter_form) != 0 && file_exists(PATH_ROOT.DS.'helpers'.DS.'QQWry.Dat'))
		{
			$query = 'SELECT a.ip' .
				' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
				$where;
		}else
		{
			$query = 'SELECT COUNT(*) AS num, a.fdtitle AS title' .
					' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
					$where.
					' GROUP BY fdtitle';
		}
		$this->_data = $this->_getArrayList( $query );
		return $this->_data;
	}
	//图像数据
	function getChartData()
	{
		$SYS = &get_instance();
		$data = $this->getItems();
		if(intval($this->_form)==0 || ($this->_fdparameter == null && !file_exists(PATH_ROOT.DS.'helpers'.DS.'QQWry.Dat')))
		{
			return $this->getChartData1($data);
		}
		if(intval($this->_form)!=0 && $this->_fdparameter == null)
		{			
			$SYS->loader->helper('ip');
			$ip = new IpLocation(PATH_ROOT.DS.'helpers'.DS.'QQWry.Dat');
			$iparray = array();
			for($i=0,$ipnum=count($data);$i<$ipnum;$i++)
			{
				$loc = $ip->getLocation($data[$i]['ip']);
				$iparray[$loc['country']]++;
			}
			arsort($iparray);
			$ipdata = array();
			$i=0;
			foreach($iparray AS $key => $val)
			{
				if($i>9) break;
				$tmp['num'] = $val;
				$tmp['title'] = $key;
				$ipdata[] = $tmp;
				$i++;
			}
			return $this->getChartData1($ipdata);
		}
		if($this->_fdparameter != null)
		{
			return $this->getChartData2();
		}
	}
	//柱状图书架
	function getChartData1($data)
	{		
		if(count($data)==0)
		{
			$SYS = &get_instance();
			$SYS->loader->setMessage(C_text::_('Item6'));
			return null;
		}
		include_once(PATH_PLUGINS.DS.'ofc'.DS.'ofc-library'.DS.'open-flash-chart.php');
		srand((double)microtime()*1000000);
		$color = array("#DFC329","#6363AC","#72B52A","#FF368D","#28A0DC");
		$title = new title( C_text::_('ToolBox') );
		//$x_legend = new x_legend( '1987 to 2009' );
		//$x_legend->set_style( '{font-size: 20px; color: #778877}' );
		//$m = new ofc_menu("#FFFFFF", "#FFFFFF");
		//$m->values(array(new ofc_menu_item_camera('Hello','save_image'), new ofc_menu_item_camera('Toggle old data','toggle')));
		$chart = new open_flash_chart();
		$chart->set_title( $title );		
		$chart->set_bg_colour( '#FFFFFF' );
		//$chart->set_x_legend( $x_legend );
		//$chart->set_menu($m);
		
		$max_Y = 0;
		for( $i=0; $i<count($data); $i++ )
		{
			$bar = new bar_3d();
			$bar->set_values( array(intval($data[$i]['num'])) );
			$bar->set_tooltip( "{$data[$i]['title']}<br>".C_text::_('Item4')."#val#" );
			$bar->set_colour( $color[$i%5] );
			$bar->set_key( $data[$i]['title'], 12 );
			$chart->add_element( $bar );
			if($max_Y < intval($data[$i]['num'])) $max_Y = intval($data[$i]['num']);
		}
		$x_axis = new x_axis();
		$x_axis->set_3d( 5 );
		$x_axis->set_colour( '#909090' );
		$x_axis->set_grid_colour( '#86BF83' );
		
		
		$x_axis->set_labels_from_array(array('')); 
		
		$y_axis = new y_axis();
		$y_axis->set_range( 0, $max_Y + 5, 5 );
		
		$chart->set_x_axis( $x_axis );
		$chart->set_y_axis( $y_axis );
		return $chart->toPrettyString();
	}
	//饼状图数据
	function getChartData2()
	{
		$tmpkey = serialize($this->_fdparameter->key);
		//筛选数据集
		$query = 'SELECT SUBSTRING(a.data,LOCATE(\''.$tmpkey.'\',a.data)+CHAR_LENGTH(\''.$tmpkey.'\')+12,'.
				' LOCATE(\';}\',a.data,LOCATE(\''.$tmpkey.'\',a.data)+CHAR_LENGTH(\''.$tmpkey.'\'))-'.
				' (LOCATE(\''.$tmpkey.'\',a.data)+CHAR_LENGTH(\''.$tmpkey.'\')+12)'.
				' )'.
				' AS data' .
				' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
				$this->_where
				;
		$data = $this->_getArrayList( $query );		
		if(count($data)==0)
		{
			$SYS = &get_instance();
			$SYS->loader->setMessage(C_text::_('Item6'));
			return null;
		}
		$value = array();
		for($i=0;$i<count($data);$i++)
		{
			$tmp = unserialize($data[$i]['data']);
			$value[] = $tmp;
		}
		$value = array_count_values($value);
		
		include_once(PATH_PLUGINS.DS.'ofc'.DS.'ofc-library'.DS.'open-flash-chart.php');
		$title = new title( C_text::_('ToolBox') );
		$chart = new open_flash_chart();
		$chart->set_title( $title );		
		$chart->set_bg_colour( '#FFFFFF' );
		$d = array();
		foreach($value as $key=>$val)
		{
			$d[] = new pie_value($val,  $key);
		}
		$pie = new pie();
		$pie->alpha(0.5);
		$pie->add_animation( new pie_fade() );
		$pie->add_animation( new pie_bounce(5) );
		$pie->start_angle( 0 );
		$pie->tooltip( '#label#:#val# ('.C_text::_('Item7').'#percent#)' );
		$pie->colours(array("#DFC329","#6363AC","#72B52A","#FF368D","#28A0DC"));
		$pie->set_values( $d );
		$chart->add_element( $pie );
		return $chart->toPrettyString();
	}
	//下拉框
	function getLists()
	{		
		return $this->_lists;
	}
	
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>